<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 会话控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\MessageService;
use common\validate\Message;
use support\ErrorCode;
use think\Request;

class MessageController extends VerifyController
{

    private $messageService;


    public function __construct()
    {
        parent::__construct();
        $this->messageService = MessageService::getInstance();
    }

    /**
     * 清除当前会话未读消息
     * @param Request $request
     * @return \think\Response
     */
    public function clearMessageUnread(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->messageService->clearMessageUnread($request->getUserId(), $param['param']['filter'], $param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送消息
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception
     */
    public function sendMessage(Request $request)
    {
        try {
            $resData = $this->messageService->sendMessage($request->param(), $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 发送系统通知消息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function sendSystemNoticeMessage(Request $request)
    {
        $param = $request->param();
        $detailData = $param['detail_data'] ?? [];
        try {
            $resData = $this->messageService->sendSystemNoticeMessage($param['data'], $detailData, $param['notify_user_mark'] ?? "producer_assistant");
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 消息列表  (包含是否已读状态)
     * @param Request $request
     * @return \think\Response
     */
    public function selectWithReadStatus(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->messageService->selectWithReadStatus($param['param'], $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 查询系统通知消息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function selectSystemNotifyMessage(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->messageService->selectSystemNotifyMessage($param['param'], $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 清空所有未读消息
     * @param Request $request
     * @return \think\Response
     */
    public function clearAllUnRead(Request $request)
    {
        try {
            $resData = $this->messageService->clearAllUnRead($request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 工单会话发送消息
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception|\Exception
     */
    public function pushWorkOrderChatMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->pushWorkOrderChatMessage($data, $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量推送工单会话消息
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception|\Exception
     */
    public function pushBatchWorkOrderChatMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->pushBatchWorkOrderChatMessage($data, $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量添加
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function addMultipleMessage(Request $request)
    {
        $data = $request->param()['data'];
        // check 检测 数据格式
        $validator = new Message();
        $validator->scene("AddMultipleMessageItem");
        foreach ($data as $item) {
            if (!$validator->check($item)) {
                throw_strack_exception("[add_multiple_message_item] " . $validator->getError(), ErrorCode::REQUEST_PARAM_ERROR);
            }
        }
        try {
            $resData = $this->messageService->addMultipleMessage($data);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 撤回消息
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function revokeMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->revokeMessage($data, $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询回复消息列表
     * @param Request $request
     * @return \think\Response
     */
    public function selectReplyMessage(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->messageService->selectReplyMessage($param['param'], $request->getUserId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询转发消息列表
     * @param Request $request
     * @return \think\Response
     */
    public function selectForwardMessage(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->messageService->selectForwardMessage($param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量转发消息
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function batchForwardMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->batchForwardMessage($data, $request->getUserId(), $request->getTenantId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 搜索聊天记录
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function searchMessageRecord(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $resData = $this->messageService->searchMessageRecord($param);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量推送系统通知消息
     * @param Request $request
     * @return \think\Response
     */
    public function batchPushSystemHitMessageV2(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->batchPushSystemHitMessageV2($data['chat_global_id_list'], $data['message_content']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 推送实体任务消息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function pushEntityTaskChatMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->pushEntityTaskChatMessage($data['chat'], $data['chat_message_content']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 批量发送消息
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function batchSendMessage(Request $request)
    {
        try {
            $data = $request->param()['data'];
            $resData = $this->messageService->batchSendMessageWithReturnChat($data, $request->getUserId(), $request->getTenantId());
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 消息卡片 稍后提醒
     * @param Request $request
     * @return \think\Response
     */
    public function remindMessageLater(Request $request)
    {
        try {
            $param = $request->param()['param'];
            $resData = $this->messageService->remindMessageLater($param);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
